package com.servlet;

import com.dao.NewsDao;
import com.dao.TypeDao;
import com.dao.UserDao;
import com.entity.News;
import com.entity.Type;
import com.entity.User;
import com.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 */
@WebServlet({"/NewsServlet.filter","/NewsAddServlet.filter","/NewsDeleteServlet.filter",
        "/NewsSelectAllServlet.filter","/NewsSelectOneServlet.filter","/NewsUpdateServlet.filter"})
public class NewsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("NewsServlet");
        String method = request.getParameter("method");
        System.out.println(method);
        if(method.equals("newsSelectList")) {
            this.selectNewsList(request, response);
        }else if (method.equals("newsSelectOne")){
            this.selectNewsOne(request, response);
        }else if (method.equals("deleteNews")){
            this.deleteNews(request, response);
        }else if (method.equals("addNews")){
            this.addNews(request, response);
        }else if (method.equals("updateNews")){
            this.updateNews(request, response);
        }else {
            response.sendRedirect(request.getContextPath()+"/NewsSelectAllServlet.filter?method=newsSelectList");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    private void selectNewsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNum = request.getParameter("pageNum");
        SqlSession session = MybatisUtil.getSession();
        NewsDao newsDao = session.getMapper(NewsDao.class);
        TypeDao typeDao = session.getMapper(TypeDao.class);
        List<News> newsList = newsDao.selectNewsAll();
        List<Type> typeList = typeDao.typeSelectAll();
        List<List> list = new ArrayList<>();
        list.add(typeList);
        list.add(newsList);
        request.setAttribute("list", list);
        request.getRequestDispatcher("/WEB-INF/page/news.jsp").forward(request, response);
    }

    private void selectNewsOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String n_id = request.getParameter("n_id");
        SqlSession session = MybatisUtil.getSession();
        NewsDao newsDao = session.getMapper(NewsDao.class);
        News news = newsDao.selectNewsOne(Integer.parseInt(n_id));
        request.setAttribute("news", news);
        request.getRequestDispatcher("/WEB-INF/page/news.jsp").forward(request, response);
    }

    private void deleteNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String n_id = request.getParameter("n_id");
        SqlSession session = MybatisUtil.getSession();
        NewsDao newsDao = session.getMapper(NewsDao.class);
        boolean b = newsDao.deleteNews(Integer.parseInt(n_id));
        session.commit();
        if (b){
            System.out.println("新闻删除成功");
            response.sendRedirect(request.getContextPath()+"/NewsSelectAllServlet.filter?method=newsSelectList");
        }
    }

    private void addNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        News news = new News();
        String n_title = request.getParameter("n_title");
        String u_name = request.getParameter("u_name");
        String t_name = request.getParameter("t_name");
        String n_content = request.getParameter("n_content");
        news.setN_title(n_title);
        news.setU_name(u_name);
        news.setT_name(t_name);
        news.setN_content(n_content);
        SqlSession session = MybatisUtil.getSession();
        NewsDao newsDao = session.getMapper(NewsDao.class);
        boolean b = newsDao.insertNews(news);
        session.commit();
        if (b) {
            System.out.println("新闻添加成功");
            response.sendRedirect(request.getContextPath() + "/NewsSelectAllServlet.filter?method=newsSelectList");
        } else {
            request.getRequestDispatcher("/WEB-INF/page/find.jsp").forward(request, response);
        }
    }

    private void updateNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        News news = new News();
        String n_id = request.getParameter("n_id");
        String n_title = request.getParameter("n_title");
        String u_name = request.getParameter("u_name");
        String t_name = request.getParameter("t_name");
        String n_content = request.getParameter("n_content");
        news.setN_id(Integer.parseInt(n_id));
        news.setN_title(n_title);
        news.setU_name(u_name);
        news.setT_name(t_name);
        news.setN_content(n_content);
        System.out.println(n_id + n_title + u_name + t_name + n_content);
        SqlSession session = MybatisUtil.getSession();
        NewsDao newsDao = session.getMapper(NewsDao.class);
        boolean b = newsDao.updateNews(news);
        session.commit();
        if (b) {
            System.out.println("新闻修改成功");
            response.sendRedirect(request.getContextPath() + "/NewsSelectAllServlet.filter?method=newsSelectList");
        }
    }
}
